<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";

// -----------------------------------------------------------------------------
// 访问相关
// -----------------------------------------------------------------------------

if (!isset($_POST['id']) || !isset($_POST['content'])) {
    exit("需要传入有效的参数。");
}

$id = $_POST["id"];
$content = $_POST["content"];

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath;
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------
// 时区设置
// -----------------------------------------------------------------------------

date_default_timezone_set($region);

$timeCreate = (string)time();
$timeEdit = $timeCreate;
$sortNumber = $timeCreate;

// -----------------------------------------------------------------------------

$title = "";

if (!$db->todoTemplateExist($id)) {
    // template 不存在，创建
    $arr = compact("title", "content", "sortNumber", "timeCreate", "timeEdit");
    $db->insertIntoTodoTemplate($arr);
    echo "模板创建成功。";
} else {
    // template 已经存在，修改
    $arr = compact("title", "content", "timeEdit");
    $db->updateTodoTemplateById($id, $arr);
    echo "模板修改成功！";
}
